package com.cloudcc.mobile.view.activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.cloudframe.util.SharePreferece;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.im.IMHelper;
import com.cloudcc.mobile.manager.CloudCCPushManager;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.service.DingWeiService;
import com.cloudcc.mobile.util.DialogUtils;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.view.login.LoginNewActivity;
import com.cloudcc.mobile.weight.SlideSwitch;
import com.cloudcc.mobile.weight.SlideSwitch.OnChangeListener;
import com.easemob.EMCallBack;
import com.litesuits.android.log.Log;
import com.umeng.message.IUmengCallback;

import java.util.HashMap;
import java.util.Map;

@Deprecated
public class SettingActivity extends BaseActivity {
	private SharePreferece share;
	private LinearLayout exit, guanyu, zhuxiao, shengyin, zhendong, time,
			huancun, weizhi, xiaoxi;
	private TextView timeset;
	private SlideSwitch locationSet, mesSet, zhengdongSet, shengyinSet;
	private Context context;
	private ImageView menubtn;
	private Builder builder;
	private String mE = RunTimeManager.getInstance().getlanguage();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		try {
			context = this;
			setupView();
			share();
			addListener();
		} catch (Exception e) {
			Tools.handle(e);
		}
	}

	@Override
	public int getLayoutId() {
		return R.layout.setting2;
	}

	private void share() {
		Map<String, ?> locationmap = share.getShare(UserManager.getManager()
				.getUser().userId + "islocation");
		Map<String, ?> pushmap = share.getShare(UserManager.getManager()
				.getUser().userId + "ispush");
		if (locationmap == null || locationmap.isEmpty()) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("time", "1200000");
			map.put("islocation", true);
			share.SaveShare(UserManager.getManager().getUser().userId
					+ "islocation", map);
		} else {
			boolean islocation = (Boolean) locationmap.get("islocation");

			String time = (String) locationmap.get("time");
			Log.d("SaveShare", "如果不是空" + islocation + time);
			if (time.equals("300000")) {
				timeset.setText("5分钟");
			} else if (time.equals("600000")) {
				timeset.setText("10分钟");
			} else if (time.equals("1200000")) {
				timeset.setText("20分钟");
			} else if (time.equals("1800000")) {
				timeset.setText("30分钟");
			} else if (time.equals("3600000")) {
				timeset.setText("1小时");
			} else if (time.equals("7200000")) {
				timeset.setText("2小时");
			}
			locationSet.setSelected(islocation);

		}
		if (!(pushmap == null || pushmap.isEmpty())) {
			boolean ispush = (Boolean) pushmap.get("ispush");
			boolean isShengyin = (Boolean) pushmap.get("isShengyin");
			boolean isZhengdong = (Boolean) pushmap.get("isZhengdong");
			Log.d("SaveShare", "如果不是空push" + ispush + isShengyin + isZhengdong);
			mesSet.setSelected(ispush);
			shengyinSet.setSelected(isShengyin);
			zhengdongSet.setSelected(isZhengdong);
		} else {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ispush", true);
			map.put("isShengyin", true);
			map.put("isZhengdong", true);
			share.SaveShare(UserManager.getManager().getUser().userId
					+ "ispush", map);
		}

	}

	/*
	 * 获取控件
	 */
	private void addListener() {
		guanyu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, CloudccActivity.class);
				startActivity(intent);
			}
		});
		/**
		 * 打开菜单
		 */
		menubtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d("SaveShare", "如果不是空addlistener");
				SettingActivity.this.finish();
			}
		});
		/**
		 * 注销登录
		 */
		zhuxiao.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showClearLogin(context);
			}
		});
		// 时间设置
		time.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if ("en".equals(mE)) {
					setEnDialog();
				}else{
					showDialog();
				}
				
			}
		});
		// 清除缓存
		huancun.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DialogUtils.showClearCache(context);
			}
		});
		// 退出
		exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DialogUtils.showExit(context);
			}
		});
		// 位置上报
		weizhi.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (locationSet.isSelected()) {
					Log.d("SaveShare", "位置上传点击改变状态");
					locationSet.setSelected(false);
				} else {
					locationSet.setSelected(true);
				}
			}
		});
		// 消息监听
		xiaoxi.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mesSet.isSelected()) {
					mesSet.setSelected(false);
				} else {
					mesSet.setSelected(true);
				}
			}

		});
		// 消息监听
		shengyin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (shengyinSet.isSelected()) {
					shengyinSet.setSelected(false);
				} else {
					shengyinSet.setSelected(true);
				}
			}
		});
		// 震动
		zhendong.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (zhengdongSet.isSelected()) {
					zhengdongSet.setSelected(false);
				} else {
					zhengdongSet.setSelected(true);
				}
			}
		});
		// 消息监听
		mesSet.setOnChangeListener(new OnChangeListener() {
			@Override
			public void onChange(SlideSwitch sb, boolean state) {
				setMessageListener(state);
			}
		});
		// 声音监听
		shengyinSet.setOnChangeListener(new OnChangeListener() {
			@Override
			public void onChange(SlideSwitch sb, boolean state) {
				setShengyinListener(state);
			}
		});
		// 震动监听
		zhengdongSet.setOnChangeListener(new OnChangeListener() {
			@Override
			public void onChange(SlideSwitch sb, boolean state) {
				setZhendongListener(state);
			}
		});

		// 位置上报
		locationSet.setOnChangeListener(new OnChangeListener() {
			@Override
			public void onChange(SlideSwitch sb, boolean state) {
				setLocationListener(state);
			}
		});
	}

	// 位置上报监听
	public void setLocationListener(boolean state) {
		Log.d("SaveShare", "位置上监听触发");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("islocation", "shifoushangbao" + state);
		String time = timeset.getText().toString().trim();
		String times = "300000";
		if (time.equals("5分钟")) {
			times = "300000";
		} else if (time.equals("10分钟")) {
			times = "600000";
		} else if (time.equals("15分钟")) {
			times = "900000";
		} else if (time.equals("30分钟")) {
			times = "1800000";
		} else if (time.equals("1小时")) {
			times = "3600000";
		}
		map.put("time", times);
		share.SaveShare(UserManager.getManager().getUser().userId
				+ "islocation", map);
		Intent intent = new Intent(context, DingWeiService.class);
		if (state) {
			AppContext.isStopLocation = false;
			context.startService(intent);
		} else {
			AppContext.isStopLocation = true;
			context.stopService(intent);
		}
	}

	// 设置震动监听
	public void setZhendongListener(boolean istrue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isZhengdong", istrue);
		share.SaveShare(UserManager.getManager().getUser().userId + "ispush",
				map);
	}

	// 设置声音监听
	public void setShengyinListener(boolean istrue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isShengyin", istrue);
		share.SaveShare(UserManager.getManager().getUser().userId + "ispush",
				map);
	}

	// 设置消息提醒监听
	public void setMessageListener(boolean istrue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ispush", istrue);
		share.SaveShare(UserManager.getManager().getUser().userId + "ispush",
				map);
		if (istrue) {//关闭后再次打开
			CloudCCPushManager.getInstance().resumePushWork();
		} else {//关闭推送
			CloudCCPushManager.getInstance().stopPushWork();
		}
	}
public void setEnDialog(){

	Builder builder = new Builder(context);
	builder.setTitle("Report time setting");
	final String[] items = new String[] { "5 minute", "10 minute", "20 minute", "30 minute",
			"1 hour", "2 hour" };
	String time = timeset.getText().toString().trim();
	int item = 0;
	if (time.equals("5 minute")) {
		item = 0;
	} else if (time.equals("10 minute")) {
		item = 1;
	} else if (time.equals("20 minute")) {
		item = 2;
	} else if (time.equals("30 minute")) {
		item = 3;
	} else if (time.equals("1 hour")) {
		item = 4;
	} else if (time.equals("2 hour")) {
		item = 5;
	}
	builder.setSingleChoiceItems(items, item,
			new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					AppContext.time = items[which];
				}
			});
	builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			timeset.setText(AppContext.time);
			Map<String, Object> map = new HashMap<String, Object>();
			String time = timeset.getText().toString().trim();
			String times = "300000";
			if (time.equals("5 minute")) {
				times = "300000";
				AppContext.locationtime = 300000;
			} else if (time.equals("10 minute")) {
				times = "600000";
				AppContext.locationtime = 600000;
			} else if (time.equals("20 minute")) {
				times = "1200000";
				AppContext.locationtime = 1200000;
			} else if (time.equals("30 minute")) {
				times = "1800000";
				AppContext.locationtime = 1800000;
			} else if (time.equals("1 hour")) {
				times = "3600000";
				AppContext.locationtime = 3600000;
			} else if (time.equals("2 hour")) {
				times = "7200000";
				AppContext.locationtime = 7200000;
			}
			map.put("time", times);
			share.SaveShare(UserManager.getManager().getUser().userId
					+ "islocation", map);

		}
	});
	builder.setNegativeButton("Cancel", null);
	builder.show();

}
	/**
	 * 设置dialog
	 */
	protected void showDialog() {
		Builder builder = new Builder(context);
		builder.setTitle("上报时间设置");
		final String[] items = new String[] { "5分钟", "10分钟", "20分钟", "30分钟",
				"1小时", "2小时" };
		String time = timeset.getText().toString().trim();
		int item = 0;
		if (time.equals("5分钟")) {
			item = 0;
		} else if (time.equals("10分钟")) {
			item = 1;
		} else if (time.equals("20分钟")) {
			item = 2;
		} else if (time.equals("30分钟")) {
			item = 3;
		} else if (time.equals("1小时")) {
			item = 4;
		} else if (time.equals("2小时")) {
			item = 5;
		}
		builder.setSingleChoiceItems(items, item,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						AppContext.time = items[which];
					}
				});
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				timeset.setText(AppContext.time);
				Map<String, Object> map = new HashMap<String, Object>();
				String time = timeset.getText().toString().trim();
				String times = "300000";
				if (time.equals("5分钟")) {
					times = "300000";
					AppContext.locationtime = 300000;
				} else if (time.equals("10分钟")) {
					times = "600000";
					AppContext.locationtime = 600000;
				} else if (time.equals("20分钟")) {
					times = "1200000";
					AppContext.locationtime = 1200000;
				} else if (time.equals("30分钟")) {
					times = "1800000";
					AppContext.locationtime = 1800000;
				} else if (time.equals("1小时")) {
					times = "3600000";
					AppContext.locationtime = 3600000;
				} else if (time.equals("2小时")) {
					times = "7200000";
					AppContext.locationtime = 7200000;
				}
				map.put("time", times);
				share.SaveShare(UserManager.getManager().getUser().userId
						+ "islocation", map);

			}
		});
		builder.setNegativeButton("取消", null);
		builder.show();
	}

	// 获取控件
	private void setupView() {
		mesSet = (SlideSwitch) findViewById(R.id.setmessage);
		locationSet = (SlideSwitch) findViewById(R.id.setloaction);
		shengyinSet = (SlideSwitch) findViewById(R.id.setshengyin);
		zhengdongSet = (SlideSwitch) findViewById(R.id.setzhengdong);
		xiaoxi = (LinearLayout) findViewById(R.id.xiaoxi);
		weizhi = (LinearLayout) findViewById(R.id.weizhi);
		exit = (LinearLayout) findViewById(R.id.exit);
		huancun = (LinearLayout) findViewById(R.id.huancun);
		zhuxiao = (LinearLayout) findViewById(R.id.zhuxiao);
		guanyu = (LinearLayout) findViewById(R.id.guanyu);
		zhendong = (LinearLayout) findViewById(R.id.zhendong);
		shengyin = (LinearLayout) findViewById(R.id.shengyin);
		time = (LinearLayout) findViewById(R.id.time);
		timeset = (TextView) findViewById(R.id.timeset);
		menubtn = (ImageView) findViewById(R.id.menu_setting);
		share = new SharePreferece(context);
		builder = new Builder(context);
	}

	/**
	 * 注销登录
	 * 
	 * @param context
	 */
	public void showClearLogin(final Context context) {
		String s1 = "亲！确认注销吗？";
		String s2 = "注销账号";
		String s3 = "确认";
		String s4 = "退出失败。。。";
		String s5 = "取消";
		if ("en".equals(mE)) {
			 s1 = "Dear! Are you sure to log out?";
			 s2 = "Logout";
			 s3 = "confirm";
			 s4 = "Exit failed...";
			 s5 = "Cancel";
		}else{
			 s1 = "亲！确认注销吗？";
			 s2 = "注销账号";
			 s3 = "确认";
			 s4 = "退出失败。。。";
			 s5 = "取消";
	}
		final String s = s5;
		Builder builder = new Builder(context);
		builder.setMessage(s1);
		builder.setTitle(s2);
		builder.setNegativeButton(s3, new AlertDialog.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// 退出环信
				IMHelper.getInstance().logout(false, new EMCallBack() {

					@Override
					public void onSuccess() {
						runOnUiThread(new Runnable() {
							public void run() {
								Intent intent = new Intent(context,
										LoginNewActivity.class);
								context.startActivity(intent);
								finish();

							}
						});
					}

					@Override
					public void onProgress(int arg0, String arg1) {

					}

					@Override
					public void onError(int arg0, String arg1) {
						Toast.makeText(mContext, s, Toast.LENGTH_SHORT)
								.show();
					}
				});

			}
		});
		builder.setPositiveButton(s5, null);
		builder.show();
	}
}
